package com.demo.crm.springboot3security.entity;

import java.time.LocalDateTime;

import com.demo.crm.springboot3security.entity.enums.FieldType;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.EnumValue;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.RelationManyToOne;
import com.mybatisflex.annotation.Table;

import lombok.Data;

/**
 * 表单字段实体
 */
@Data
@Table("form_fields")
public class FormField {
    @Id
    private Long id;

    private String formId; // 字段名称

    @RelationManyToOne(selfField = "formId", targetField = "id")
    private CustomForm form;

    @Column
    private String name; // 字段名称

    @Column
    private String code; // 字段编码

    // @Column(typeHandler = CompositeEnumTypeHandler.class)
    @EnumValue
    private FieldType type; // 字段类型

    @Column
    private String label; // 字段标签

    private String placeholder; // 占位文本

    private String options; // 选项配置（JSON格式，用于下拉框、单选、多选等）

    private boolean required; // 是否必填

    private boolean customized; // 是否自定义

    private String validation; // 验证规则（JSON格式）

    private Integer orderNum; // 排序号

    @Column("default_value")
    private String defaultValue; // 默认值

    private String helpText; // 帮助文本

    private String styleConfig; // 样式配置（JSON格式）

    private LocalDateTime createdAt;

    private LocalDateTime updatedAt;

    public void prePersist() {
        createdAt = LocalDateTime.now();
        updatedAt = LocalDateTime.now();
    }

    public void preUpdate() {
        updatedAt = LocalDateTime.now();
    }
}